package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.JsonResult;

import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){
        model.addAttribute("pageInfo", permissionService.query(qo));
        return "permission/list"; // 找视图 /WEB-INF/views/permission/list.jsp
    }

    @RequestMapping("/delete")
    public String delete(Long id){
        if (id != null) {
            permissionService.delete(id);
        }
        return "redirect:/permission/list.do"; // 让重新发一次请求
    }

    // 去新增或去修改
    @RequestMapping("/input")
    public String input(Long id, Model model){
        if (id != null) { // 表示去修改
            Permission permission = permissionService.get(id);
            model.addAttribute("permission", permission);
        }
        return "permission/input"; // 让重新发一次请求
    }

    // 新增或修改
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Permission permission){
        if(permission.getId()  == null){ // 新增
            permissionService.save(permission);
        }else {
            permissionService.update(permission); // 修改
        }
        return "redirect:/permission/list.do"; // 让重新发一次请求
    }

    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload(){
        try {
            permissionService.reload();
            return new JsonResult(); //success默认为true
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(false,"加载失败");
    }

}
